<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * Ham load nhanh view tuong duong $this->load->view()
 * @param string $template
 * @param array $data
 */
function view_load($template, $data=null)
{
    if(!file_exists(APPPATH . 'views/' . $template . '.php')) return;
    $CI = & get_instance();
    if(is_array($data)) $CI->load->view($template, $data);
    $CI->load->view($template);
}

function striplongstr($str, $max_str = 18) {
//	$max_str=18;
	return strlen($str) > $max_str ? substr($str, 0, $max_str - 3) . '...' : $str;
}

function time2str($time) {
	$CI = &get_instance();
	$CI->load->helper('date');
//	return timespan(0,now()-$time) . lang ('ago');
	$words = array(
		'second' => 60,
		'minute' => 60,
		'hour' => 24,
		'day' => 7,
		'week' => 29,
	);
	$sub = now() - $time;
	if (!ini_get('date.timezone')) {
		date_default_timezone_set('GMT');
	}
	$result = lang('on_date') . mdate($CI->config->item('CI_date_format'), $time);
	foreach ($words as $word => $key) {
//		echo $key . ' - ';
		if ($sub % $key == 1)
			$result = $sub % $key . lang($word) . lang('ago');
		else if ($sub % $key > 1)
			$result = $sub % $key . lang($word . 's') . lang('ago');
		$sub = (int) ($sub / $key);
	}
	return $result;
}

function youtube_embed($video_link)
{
	$youtube_reg = '/https?:\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([A-Za-z0-9_-]+)(&(amp;)?[\w\?=]*)?/';
	$matches = array();
	preg_match($youtube_reg, $video_link, $matches);
	if(count($matches)<2) return;
	$video_id = $matches[1];
	return 'http://www.youtube.com/v/' . $video_id . '?version=3&amp;autohide=1';
}
?>
